package com.xmzj.help;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.TextView;

import com.xmzj.R;
import com.xmzj.mvp.utils.ToastUtil;


/**
 * 弹框类都放到这里
 */
public class DialogHelper {
    private BaseDialog dialog;
    private static DialogHelper sInstance;

    public interface DialogBtnListener{
        void clickSureBtn(String text);
    }
    /**
     * 做成单例模式
     */
    public static DialogHelper getInstance() {
        if (sInstance == null) {
            synchronized (DialogHelper.class) {
                if (sInstance == null) {
                    sInstance = new DialogHelper();
                }
            }
        }
        return sInstance;
    }


    /**
     * 修改昵称弹框
     */
    public BaseDialog modifyNickDialog(Context context, String content,DialogBtnListener dialogBtnListener) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = new BaseDialog(context, R.style.CustomConfirmDialog);
        dialog.setContentView(R.layout.dialog_modify_nick);
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.setCancelable(false);
        EditText mContentEt = dialog.findViewById(R.id.content_et);
        TextView mBtnCancelConfirm = dialog.findViewById(R.id.cancel_tv);
        TextView mBtnSureProtocol = dialog.findViewById(R.id.sure_tv);
        mContentEt.setText(content);
        mContentEt.setSelection(content.length());
        mBtnCancelConfirm.setOnClickListener(view -> dialog.dismiss());
        mBtnSureProtocol.setOnClickListener(view -> {
            String nickValue = mContentEt.getText().toString();
            if(TextUtils.isEmpty(nickValue)){
                ToastUtil.showToast(context,"昵称不能为空！");
            }else {
                if(dialogBtnListener!=null){
                    dialogBtnListener.clickSureBtn(nickValue);
                    dialog.dismiss();
                }
            }
        });
        return dialog;
    }

    /**
     * 修改手机号码弹框
     */
    public BaseDialog modifyPhoneDialog(Context context, String content,DialogBtnListener dialogBtnListener) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = new BaseDialog(context, R.style.CustomConfirmDialog);
        dialog.setContentView(R.layout.dialog_phone_nick);
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.setCancelable(false);
        EditText mContentEt = dialog.findViewById(R.id.content_et);
        TextView mBtnCancelConfirm = dialog.findViewById(R.id.cancel_tv);
        TextView mBtnSureProtocol = dialog.findViewById(R.id.sure_tv);
        mContentEt.setText(content);
        mContentEt.setSelection(content.length());
        mBtnCancelConfirm.setOnClickListener(view -> dialog.dismiss());
        mBtnSureProtocol.setOnClickListener(view -> {
            String nickValue = mContentEt.getText().toString();
            if(TextUtils.isEmpty(nickValue)){
                ToastUtil.showToast(context,"手机号码不能为空！");
            }else {
                if(dialogBtnListener!=null){
                    dialogBtnListener.clickSureBtn(nickValue);
                    dialog.dismiss();
                }
            }
        });
        return dialog;
    }
    /**
     * 修改邮箱弹框
     */
    public BaseDialog modifyMailDialog(Context context, String content,DialogBtnListener dialogBtnListener) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = new BaseDialog(context, R.style.CustomConfirmDialog);
        dialog.setContentView(R.layout.dialog_mail_nick);
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.setCancelable(false);
        EditText mContentEt = dialog.findViewById(R.id.content_et);
        TextView mBtnCancelConfirm = dialog.findViewById(R.id.cancel_tv);
        TextView mBtnSureProtocol = dialog.findViewById(R.id.sure_tv);
        mContentEt.setText(content);
        mContentEt.setSelection(content.length());
        mBtnCancelConfirm.setOnClickListener(view -> dialog.dismiss());
        mBtnSureProtocol.setOnClickListener(view -> {
            String nickValue = mContentEt.getText().toString();
            if(TextUtils.isEmpty(nickValue)){
                ToastUtil.showToast(context,"手机号码不能为空！");
            }else {
                if(dialogBtnListener!=null){
                    dialogBtnListener.clickSureBtn(nickValue);
                    dialog.dismiss();
                }
            }
        });
        return dialog;
    }


}
